
import { baseURL, clientId } from 'common/constant.js'
import axios from 'axios'
import store from '@/store.js'


import { Toast } from 'mint-ui';


class Http {
  constructor (baseURL, authorization, contentType) {
    this.baseURL = baseURL
    this.authorization = authorization

    this.contentType = contentType

    this.unwatch = store.watch(function (state, getter) {
      //todo: 监听 Authorization

    }, (newAuth) => { this.authorization = newAuth })
  }

  set baseURL (newValue) {
    axios.defaults.baseURL = newValue
  }

  set authorization (newValue) {
    if (newValue) {
      axios.defaults.headers.common['Authorization'] = JSON.stringify(newValue)
    }
  }

  set contentType (newValue) {
    if (newValue) {
      axios.defaults.headers.post['Content-Type'] = newValue
    }
  }

  destroy () {
    this.unwatch()
  }


  /**
   * 发送请求
   * @param urlPath : string    url路径
   * @param method  : string    请求方式
   * @param data  : Object    请求的参数 或 数据
   * @param headers  : Object    请求头对象
   * @param disableSuccessPrompt : boolean    是否禁用全局的成功提示
   * @param disableFailPrompt  : boolean    是否禁用全局的失败提示
   * @returns AxiosPromise    请求的 AxiosPromise 对象
   */
  request(urlPath,method,data,headers,disableSuccessPrompt,disableFailPrompt){

    let reqConfig = {
      url:urlPath,
      method:method
    };

    if (method == "post" || method == "put" || method == "patch"){
      reqConfig.data = data;
    }else {
      reqConfig.params = data;
    }

    if (headers){
      reqConfig.headers = headers;
    }


    let axiosPromise = axios(reqConfig);

    if (!disableSuccessPrompt) {
      axiosPromise = axiosPromise.then(function (response) {
        //todo: 成功的提示弹窗
        Toast("成功了");
        return response;
      });
    }

    if (!disableFailPrompt) {
      axiosPromise = axiosPromise.catch(function (error) {
        // todo: 失败的提示弹窗
        Toast("失败了");
        throw error;
      });
    }

    return axiosPromise

  }


  /**
   * 发送get请求
   * @param urlPath : string    url路径
   * @param params  : Object    请求的参数
   * @param disableSuccessPrompt : boolean    是否禁用全局的成功提示
   * @param disableFailPrompt  : boolean    是否禁用全局的失败提示
   * @returns AxiosPromise    请求的 AxiosPromise 对象
   */
  get(urlPath,params,disableSuccessPrompt,disableFailPrompt){
    return this.request(urlPath,"get",params,null,disableSuccessPrompt,disableFailPrompt);
  }



  /**
   * 发送post请求
   * @param urlPath : string    url路径
   * @param data  : Object    请求的数据
   * @param contentType  : string    请求头的'Content-Type'字段的值
   * @param disableSuccessPrompt : boolean    是否禁用全局的成功提示
   * @param disableFailPrompt  : boolean    是否禁用全局的失败提示
   * @returns AxiosPromise    请求的 AxiosPromise 对象
   */
  post(urlPath,data,contentType,disableSuccessPrompt,disableFailPrompt){
    let headers = {'Content-Type':contentType};
    return this.request(urlPath,"post",data,headers,disableSuccessPrompt,disableFailPrompt);
  }




}



//todo: 这是临时的initAuth
const initAuth = {
  client_id: clientId, // 请求
  requesttype: 4, // 请求
  requestversion: '1.2',
  token: ''
};

const contentType = 'application/x-www-form-urlencoded' ;

const http = new Http(baseURL, initAuth,contentType)

export default http
